<template>
	<view class="body">
		<xk-header title="开票详情"></xk-header>
		<view class="util-bar" v-for="(item,index) in data" :key="index">
			<view class="estate-box">
				<u-row customStyle="margin-bottom:15px">
					<u-col span="12"><view class="danhao-text">收录明细编号：{{item.project_no}}</view></u-col>
				</u-row>
				<u-row customStyle="margin-bottom:8px">
					<u-col span="5"><view class="grey-text">所属开票id:</view></u-col>
					<u-col span="7"><view class="black-text">{{item.pi_invoic_id}}</view></u-col>
				</u-row>
				<u-row customStyle="margin-bottom:8px">
					<u-col span="5"><view class="grey-text">申请人姓名:</view></u-col>
					<u-col span="7"><view class="black-text">{{applyerName}}</view></u-col>
				</u-row>
				<u-row customStyle="margin-bottom:8px">
					<u-col span="5"><view class="grey-text">项目类型:</view></u-col>
					<u-col span="7" v-if="item.project_type == 5"><view class="black-text">租金</view></u-col>
					<u-col span="7" v-if="item.project_type == 10"><view class="black-text">管理费</view></u-col>
					<u-col span="7" v-if="item.project_type == 15"><view class="black-text">电费</view></u-col>
					<u-col span="7" v-if="item.project_type == 20"><view class="black-text">水费</view></u-col>
					<u-col span="7" v-if="item.project_type == 25"><view class="black-text">热水费</view></u-col>
					<u-col span="7" v-if="item.project_type == 30"><view class="black-text">空调冷量费</view></u-col>
					<u-col span="7" v-if="item.project_type == 35"><view class="black-text">空调热量费</view></u-col>
					<u-col span="7" v-if="item.project_type == 40"><view class="black-text">有偿服务费</view></u-col>
					<u-col span="7" v-if="item.project_type == 45"><view class="black-text">其他</view></u-col>
				</u-row>
				<u-row customStyle="margin-bottom:8px">
					<u-col span="5"><view class="grey-text">项目名称:</view></u-col>
					<u-col span="7"><view class="black-text">{{item.project_name}}</view></u-col>
				</u-row>
				<u-row customStyle="margin-bottom:8px">
					<u-col span="5"><view class="grey-text">单价:</view></u-col>
					<u-col span="7"><view class="black-text">{{item.project_price}}元</view></u-col>
				</u-row>
				<u-row customStyle="margin-bottom:8px">
					<u-col span="5"><view class="grey-text">金额:</view></u-col>
					<u-col span="7"><view class="black-text">{{item.project_money}}元</view></u-col>
				</u-row>
				<u-row customStyle="margin-bottom:8px">
					<u-col span="5"><view class="grey-text">备注:</view></u-col>
					<u-col span="7"><view class="black-text">{{item.remark}}</view></u-col>
				</u-row>

			</view>
		</view>

		<!-- 开票受理 -->
		<view class="util-bar" v-if="allData.invoic_status == 20 || allData.invoic_status == 30">
			<view class="estate-box">
				<u-row customStyle="margin-bottom:15px">
					<u-col span="12"><view class="danhao-text">开票受理信息填写：</view></u-col>
				</u-row>
				<u--form
				labelPosition="left"
				:model="formData"
				ref="uviewForm"
				>
					<u-form-item
						label="发票信息:"
						prop="invoic_info"
						borderBottom
						ref="invoic_info"
						labelWidth="120"
					>

					</u-form-item>
					<u--textarea v-model="formData.invoic_info" placeholder="请输入发票信息" height="50" count confirmType="done"></u--textarea>
				</u--form>
			</view>
		</view>


		<!-- 开票登记 -->
		<view class="util-bar" v-if="allData.invoic_status == 40">
			<view class="estate-box">
				<u-row customStyle="margin-bottom:15px">
					<u-col span="12"><view class="danhao-text">开票登记信息填写：</view></u-col>
				</u-row>
				<u--form
				labelPosition="left"
				:model="formData"
				ref="uviewForm"
				>
					<u-form-item
						label="发票登记备注:"
						prop="invoic_info"
						borderBottom
						ref="invoic_info"
						labelWidth="120"
					>

					</u-form-item>
					<u--textarea v-model="formData.register_remark" placeholder="请输入登记备注" height="50" count confirmType="done" style = "margin-bottom: 20px;"></u--textarea>

					<u-form-item
						label="上传发票文件:"
						prop="invoic_text"
						borderBottom
						ref="invoic_text"
						labelWidth="100"
					>
						<u-upload
								:fileList="fileList1"
								@afterRead="afterRead"
								@delete="deletePic"
								name="1"
								multiple
								:maxCount="10"
								width="70"
								height="70"
							></u-upload>
					</u-form-item>
				</u--form>
			</view>
		</view>

		<!-- 按钮 -->
		<view v-if="allData.invoic_status == 20 || allData.invoic_status == 30">
			<view class="common-bottom-view">
				<view class="common-btn btn-plant btn" @click="acceptFeedback(20)">拒绝受理</view>
				<view class="common-btn btn" @click="acceptFeedback(10)">同意受理</view>
			</view>
		</view>
		<view v-if="allData.invoic_status == 40">
			<view class="common-bottom-view">
				<view @click="acceptFeedback()" class="common-btn">登记</view>
			</view>
		</view>
		<view class="common-zw"></view>
	</view>
</template>

<script>
export default{
	data(){
		return{
			applyerName:'',
			fileList1:[],
			cancelData:{
				pi_invoic_id:'',
				cancel_content:'',
				cancel_date:'',
				isSubmit:''
			},
			allData:'',
			invoicId:'',
			data:[],
			status:'',
			invoic:'',
			formData: {
				pi_invoic_id: '',
				receiver_results: '',
				receiver_content: '',
				invoic_info: '',
				invoic_text:'',
				register_remark:'',
				isSubmit: false
			}
		};
	},
	onLoad(options) {
		this.invoicId = JSON.parse(options.invoicId)
		console.log(this.invoicId)
	},
	onShow(){
		this.getInvoicDetail()
	},
	methods:{
		// 删除图片
		deletePic(event) {
			this[`fileList${event.name}`].splice(event.index, 1)
		},
		// 新增图片
		async afterRead(event) {
			// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
			console.log("name:",event.name)
			let lists = [].concat(event.file)
			let fileListLen = this[`fileList${event.name}`].length
			lists.map((item) => {
				this[`fileList${event.name}`].push({
					...item,
					status: 'uploading',
					message: '上传中'
				})
			})
			for (let i = 0; i < lists.length; i++) {
				const result = await this.$util.uploadFilePromise(lists[i].url)
				this.$util.uploadFilePromise(lists[i].url).then((res) => {
					console.log("传过来的数据:",res.result)
					this.formData.invoic_text += res.result + ";"
					console.log("发票文件数组：",this.formData.invoic_text)
				})

				let item = this[`fileList${event.name}`][fileListLen]
				this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
					status: 'success',
					message: '',
					url: result
				}))
				fileListLen++
			}
		},

		getInvoicDetail(){
			this.$api.tickets_apply_detail(this.invoicId).then((res) => {
				console.log("hello：",res)
				this.allData = res.piInvoicData_getPiInvoic
				this.applyerName = res.piInvoicData_getPiInvoic.apply_user_name
				this.data = res.piInvoicData_getPiInvoic.dets
				console.log("全部信息：",this.allData)
				// this.status = res.piInvoicData_getPiInvoic.invoic_status
				// console.log("状态：",this.status)
			})
		},
		acceptFeedback(accept_result){
			this.formData.pi_invoic_id = this.invoicId
			if(accept_result == 10){
				this.formData.receiver_content = "同意"
			}
			else if(accept_result == 20){
				this.formData.receiver_content = "拒绝"
			}
			if(this.allData.invoic_status == 20 || this.allData.invoic_status == 30){
				this.$util.confirm({
					content: "确定提交受理？",
					confirmText:"确定"
				}).then(isConfirm => {
					if (isConfirm) {
						this.formData.receiver_results = accept_result
						console.log("提交结果：",this.formData)
						this.$util.post({
							url: "app/piInvoic/accept",
							data: this.formData
						}).then((res)=>{
							uni.redirectTo({
								url:'/pagesB/open-ticket/ticket-apply-finish?type=apply'
							})
						});
					}
				})
			}
			else if(this.allData.invoic_status == 40){
				this.$util.confirm({
					content: "确定进行登记？",
					confirmText:"确定"
				}).then(isConfirm => {
					if (isConfirm) {
						console.log("提交结果2：",this.formData)
						this.$util.post({
							url: "app/piInvoic/register",
							data: this.formData
						}).then((res)=>{
							uni.redirectTo({
								url:'/pagesB/open-ticket/ticket-apply-finish?type=apply'
							})
						});
					}
				})
			}
		},
	}
}
</script>

<style lang="scss" scoped>
.body{
	background:#F8F8F8;
}
.estate-box{
	background:#FFFFFF;
	padding:10px 10px 10px 10px;
	margin:10px;
	border-radius:6px;
	font-size:28rpx;
}
.danhao-text{
	font-size:32rpx;
	font-weight:bold;
}
.grey-text{
	color:#999999;
}
.black-text{
	padding-left:5px;
}
</style>
